package org.bambrikii.photobank.web.mailing.beans;

import java.util.List;
import java.util.Map;

import javax.activation.DataSource;

import org.bambrikii.photobank.business.managers.TemplateManager;
import org.bambrikii.photobank.mailing.core.managers.MailManager;
import org.bambrikii.photobank.mailing.model.Template;
import org.bambrikii.site.base.auth.model.User;
import org.bambrikii.site.base.business.managers.ConfigurationManager;
import org.bambrikii.site.base.business.managers.core.ManagerException;
import org.bambrikii.site.base.struts2.core.SiteBaseBean;
import org.bambrikii.site.base.struts2.mailing.beans.MailingBean;

public class MailingBeanImpl extends SiteBaseBean implements MailingBean {
	private static final String SERVICE_EMAIL_FROM = ".ru.psfotos.mailing.service.from";
	private static final String SERVICE_EMAIL_HOST = ".ru.psfotos.mailing.service.mailHost";
	private static final String SERVICE_LOGIN_USERNAME = ".ru.psfotos.mailing.service.auth.username";
	private static final String SERVICE_LOGIN_PASSWORD = ".ru.psfotos.mailing.service.auth.password";

	private ConfigurationManager configurationManager;
	private MailManager mailManager;
	private TemplateManager templateManager;

	public MailingBeanImpl(ConfigurationManager configurationManager, TemplateManager templateManager) {
		this.configurationManager = configurationManager;
		this.templateManager = templateManager;
	}

	protected MailManager getMailManager() throws ManagerException {
		if (mailManager == null) {
			org.bambrikii.photobank.mailing.core.model.Configuration cfg = new org.bambrikii.photobank.mailing.core.model.Configuration();
			cfg.setFrom(configurationManager.getCacheConfigValue(SERVICE_EMAIL_FROM));
			cfg.setMailHost(configurationManager.getCacheConfigValue(SERVICE_EMAIL_HOST));
			cfg.setRequiresAuthentication(true);
			cfg.setUsername(configurationManager.getCacheConfigValue(SERVICE_LOGIN_USERNAME));
			cfg.setPassword(configurationManager.getCacheConfigValue(SERVICE_LOGIN_PASSWORD));

			mailManager = new MailManager(cfg);
		}
		return mailManager;
	}

	@Override
	public void email(String templateKey, String locale, String to, Map<String, Object> params) throws Exception {
		Template tpl = templateManager.getByKey(templateKey, locale);
		getMailManager().send(tpl.getSubject(), tpl.getBody(), tpl.getContentType().getValue(), to, params,
				tpl.getKey());

	}

	@Override
	public void email(String templateKey, String locale, String to, List<DataSource> attachments,
			Map<String, Object> params) throws Exception {
		Template tpl = templateManager.getByKey(templateKey, locale);
		getMailManager().send(tpl.getSubject(), tpl.getBody(), tpl.getContentType().getValue(), to, attachments,
				params, tpl.getKey());
	}

	@Override
	public String getLocale(User user) {
		return (user.getLanguage() != null && user.getLanguage().getLocale() != null) ? user.getLanguage().getLocale()
				: null;
	}
}
